
faApp.controller('PersonalSupplierListCtrl',['$scope','personalSupplierService','exportService','$location','$controller','$route','$routeParams','sessionService',
  function($scope,personalSupplierService,exportService,$location,$controller,$route,$routeParams,sessionService) {
  
  $controller('BaseListCtrl', {$scope: $scope}); 

  $scope.searchMap = {
    "loginName" : "",
    "name" : "",
    "mobile" : "",
    "companyName" : "",
    "companyAddress" : "",
    "checkStatus" : ""
  };
  $scope.config = {
    "pageSize" : 20,
    "pageNo" : 1
  };
  $scope.$watch('config',function(){
    personalSupplierService.personalSupplierList($scope.config).then(function(json){
    $scope.productSupplierList = json.productSupplierList;
    $scope.pager = json.pager;
    $scope.loading = false;
    });
  },true) 


  $scope.pageChanged = function(){
    $scope.config.pageNo = $scope.pager.pageNo;
  };
  $scope.exportCsv = function(){
    exportService.exportCSV($scope.searchMap,"PERSONAL_SUPPLIER_LIST_CSV");
  };
}]);



faApp.controller('PersonalSupplierDetailCtrl',['$scope','personalSupplierService','$location','$controller','$route','$routeParams','sessionService',
  function($scope,personalSupplierService,$location,$controller,$route,$routeParams,sessionService) {
  
  $controller('BaseListCtrl', {$scope: $scope}); 
  $scope.id = $routeParams.id;
  $scope.save = {};
  $scope.black = {};
  $scope.visit = {};
  personalSupplierService.PersonalSupplierInfo({id:$scope.id}).then(function(json){
    $scope.bean = json.bean || {};
    $scope.save.companyName = $scope.bean.companyName;
    $scope.save.id = $scope.bean.id;
    $scope.save.companyAddress = $scope.bean.companyAddress;
    $scope.save.companyPosition = $scope.bean.companyPosition;
    $scope.save.emailAddress = $scope.bean.emailAddress;
    $scope.save.auditRemarks = $scope.bean.auditRemarks;
    $scope.save.checkStatus = $scope.bean.checkStatus;

    if($scope.save.checkStatus == '4' || $scope.save.checkStatus == '5'){
      $scope.save.uRole = "2";
    }else if($scope.save.checkStatus == '3'){
      $scope.save.uRole = "99";
    }
    if(!$scope.bean.blackList){
      $scope.bean.blackList = "2";
    }
  });

  $scope.selectLevelType = function(type){
    $scope.save.checkStatus = type;
    $scope.save.uRole = "2";
    $scope.aoMsg = false;
  };
  $scope.selectRoleResult = function(){
    $scope.save.checkStatus = "";
    if ($scope.save.uRole == "99"){
      $scope.save.checkStatus = "3";
    }
  }

  $scope.auditOption = function(){
    if($scope.save.failReson == "3"){
      $scope.aoMsg = true;
    }else{
      $scope.aoMsg = false;
    }
  };

  $scope.personalSupplierSave = function(){
    if (!$scope.certform.$invalid){
      if (!$scope.save.uRole || ($scope.save.uRole == "2" && !$scope.save.checkStatus)){
        alert("请选择评级");
        return;
      }
      if($scope.aoMsg){
        if(!$scope.auditOptionMessage){
          alert("请填写未通过原因！");
          return;
        }
        $scope.save.failReson = $scope.auditOptionMessage;
      }
      personalSupplierService.PersonalSupplierSave($scope.save).then(function(json){
        if (!json.statusCode){
          $scope.addAlert("操作成功");
          $route.reload();
        }else{
          alert("出错了:" + json.statusCode);
        }  
      });
    }else{
      alert("请先完整填写信息");
      return;
    }
  };
  $scope.showBlackAddForm = function(id){
    $scope.black.id = id;
    $scope.showBlackAddFormFlag = true;
  };
  $scope.showBlackDelForm = function(id){
    $scope.black.id = id;
     $scope.black.blackList = "2";
    personalSupplierService.PersonalSupplierSave($scope.black).then(function(json){
    if (!json.statusCode){
      $scope.addAlert("操作成功");
      $route.reload();
    }else{
      alert("出错了:" + json.statusCode);
    }  
  });
  };
  $scope.blackAdd = function(){
    if (!$scope.form.$invalid){
      $scope.black.blackList = "1";
     personalSupplierService.PersonalSupplierSave($scope.black).then(function(json){
        if (!json.statusCode){
          $scope.addAlert("操作成功");
          $route.reload();
        }else{
          alert("出错了:" + json.statusCode);
        }  
      });
    }else{
      alert("请先完整填写信息");
      return;
    }
  };
    /**回访**/
  $scope.showVisitLayer = function(id){
    $scope.visit.id = id;
    $scope.visit.faId = id;
    $scope.visit.type = "2";
    personalSupplierService.PersonalSupplierVisit($scope.visit).then(function(json){
      $scope.visitList = json.list;
    });
    $scope.isShowVisitLayer = true;
  }

    $scope.addPersonalSupplierVisit = function(){
    $scope.visit.flagVisit = "1";
        /**修改已拜访状态**/ 
    personalSupplierService.PersonalSupplierSave($scope.visit).then(function(json){
      if (!json.statusCode){
        $scope.addAlert("操作成功");
        $route.reload();
      }else{
        alert("出错了:" + json.statusCode);
      }  
    });

    /**添加拜访**/ 
    personalSupplierService.PersonalSupplierAddVisit($scope.visit).then(function(json){
      if (!json.statusCode){
        personalSupplierService.PersonalSupplierVisit($scope.visit).then(function(json){
          $scope.visitList = json.list;
        });
        $scope.visit.visitRemark = "";
      }else{
        alert("出错了:" + json.statusCode);
      }  
    });
  }
}]);